package com.fortune.controller.app;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.dal.dao.AccountDAO;
import com.fortune.dal.dao.TradeQueryDAO;
import com.fortune.dal.model.Account;
import com.fortune.dal.model.Cust;
import com.fortune.dal.model.TradeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("appasset")
public class AppAssetController {

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private AccountDAO accountDAO;

    @Autowired
    private TradeQueryDAO tradeQueryDAO;


    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws BusinessException {
        ModelAndView mv = new ModelAndView("wealthcenter");

        Cust cust = (Cust) httpSession.getAttribute("user");
        Account account = new Account();
        account.setCustId(cust.getId());
        //账户余额
        try {
            List<Account> list = accountDAO.queryByModel(account);
            Account param = list.get(0);
            Double allBalance = param.getAccountBalance();
            httpSession.setAttribute("allBalance",allBalance);
            mv.addObject("allBalance",allBalance);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //交易记录
        List<Map<String,Object>> list = new ArrayList<>();
        try {
            List<TradeQuery> lists = tradeQueryDAO.queryByModel(new TradeQuery());
            if(ListUtils.isEmpty(lists)){
                mv.addObject("message", "暂无交易记录");
            }

            Cust user = (Cust)httpSession.getAttribute("user");

            for(TradeQuery tradeQuery : lists){
                if(user.getId().equals(tradeQuery.getCustId())){
                    Map<String,Object> tradeMap = MapUtils.parseMap(tradeQuery);
                    if(tradeQuery.getTradeOperator().equals("取现")){
                        tradeMap.put("iconUrl","../images/icon-11.png");
                    }else if(tradeQuery.getTradeOperator().equals("充值")){
                        tradeMap.put("iconUrl","../images/icon-12.png");
                    }else{
                        tradeMap.put("iconUrl","../images/icon-13.png");
                    }

                    list.add(tradeMap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mv.addObject("list", list);
        mv.addObject("menuFlag", "appasset");

        return mv;
    }


    @RequestMapping(value = "myassets", method = RequestMethod.GET)
    public ModelAndView myAssets() throws BusinessException {
        ModelAndView mv = new ModelAndView("myassets");

        return mv;
    }
}
